
package com.ruoyi.client.service.impl;

import com.github.xiaoymin.knife4j.core.util.StrUtil;
import com.ruoyi.client.mapper.GoodsMapper;
import com.ruoyi.client.pojo.form.GoodsQueryPageForm;
import com.ruoyi.client.pojo.vo.GoodsDetailVo;
import com.ruoyi.client.pojo.vo.GoodsVo;
import com.ruoyi.client.service.GoodsService;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.myshop.domain.Goods;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("ClientGoodsServiceImpl")
public class GoodsServiceImpl implements GoodsService {

    @Resource(name = "ClientGoodsMapper") //注入指定命名的Bean
    private GoodsMapper goodsMapper;

    //分页查询
    @Override
    public List<GoodsVo> queryPage(GoodsQueryPageForm form) {
        return goodsMapper.queryPage(form);
    }

    //查询商品详情 ById
    @Override
    public GoodsDetailVo selectVoById(Integer id) {

        Goods goods = goodsMapper.selectVoById(id);
        if(goods==null) return null ;  //商品是否存在

        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        BeanUtils.copyProperties(goods,goodsDetailVo); //调用框架工具类，复制Bean,二者数据类型要一致，Long不能赋值Integer的

        //转换商品轮播图数据格式
        String album = goods.getAlbum();
        if (StrUtil.isNotBlank(album)) {
            //以竖线为分隔符，则split的时候需要加上两个斜杠【\\】进行转义
            String[] albumArray = album.split("\\|");
            goodsDetailVo.setAlbum(albumArray);
        } else {
            goodsDetailVo.setAlbum(null);
        }
        return goodsDetailVo;
    }

    //查询商品对象 ById
    @Override
    public Goods selectByPrimaryKey(Long id) {
        return goodsMapper.selectByPrimaryKey(id);
    }
}
